package com.cw.controller;


import com.cw.beans.User;
import com.cw.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.mail.*;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import java.util.Properties;
import java.util.Random;

@Controller
public class RegisterController {
    @Autowired
    private UserMapper userMapper;

    @RequestMapping("/getregister")
    public String getRegister(String username,String password,String email,String email1,Model model) {
        if(email1.equals(num)){
            //可以注册
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
            user.setEmail(email);
            user.setName(username);
            User userByUserName = userMapper.getUserByUserName(username);
            if(userByUserName==null){
                userMapper.registeruser(user);
                //注册成功
                System.out.println("注册成功");
                model.addAttribute("msg","注册成功");
            }else {
                model.addAttribute("msg","账号已存在,请重新注册!");
                System.out.println("账号已存在,请重新注册!");
                return "register";
            }
        }else {
            System.out.println("验证码错误");
            model.addAttribute("msg","验证码输出有误，请重新注册!!");
            return "register";
        }
        return "index";
    }
    String num = "";
    @RequestMapping("/getemail")
    public void getPhone(String email,Model model) {
        Random random = new Random();
        for(int i = 0; i < 4; i++){
            num += random.nextInt(10);
        }
        System.out.println(num);
        // 发件人邮箱和授权码
        final String from = "1462638455@qq.com"; // 替换为你的QQ邮箱
        final String pwd = "rvogmpbdviikbagb"; // 替换为你的SMTP授权码

        // 收件人邮箱
        String to = email; // 替换为收件人邮箱

        // QQ邮箱的SMTP服务器地址
        String host = "smtp.qq.com";

        // 设置属性
        Properties properties = System.getProperties();
        properties.setProperty("mail.smtp.host", host); // 设置SMTP服务器
        properties.setProperty("mail.smtp.port", "587"); // 设置端口
        properties.setProperty("mail.smtp.auth", "true"); // 需要认证
        properties.setProperty("mail.smtp.starttls.enable", "true"); // 启用TLS加密

        // 获取默认的Session对象
        Session session = Session.getDefaultInstance(properties, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(from, pwd); // 设置发件人邮箱和授权码
            }
        });

        try {
            // 创建MimeMessage对象
            MimeMessage message = new MimeMessage(session);
            // 设置发件人
            message.setFrom(new InternetAddress(from));
            // 设置收件人
            message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
            // 设置邮件主题
            message.setSubject("验证码");
            // 设置邮件正文
            message.setText("您的邮箱验证码为: "+num);

            // 发送邮件
            Transport.send(message);
            System.out.println("邮件发送成功！");
        } catch (MessagingException e) {
            e.printStackTrace();
            System.out.println("邮件发送失败：" + e.getMessage());
        }
        model.addAttribute("num",num);
    }
}
